﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CompilationFactures.Objects;
using CompilationFactures.Controls;

namespace CompilationFactures
{
    public partial class FormulairePrincipale : Form
    {
        ucFacture ControleFacture = new ucFacture();
        ucArticles ControleArticle = new ucArticles();
        List<Facture> ListeFacture;

        
        public FormulairePrincipale()
        {
            InitializeComponent();

            this.Controls.Add(ControleFacture);
            this.Controls.Add(ControleArticle);

            ControleFacture.BringToFront();
            ControleFacture.Dock = DockStyle.Fill;
            ControleArticle.Dock = DockStyle.Fill;
        }

        private void chargerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialogFactures.FileName = String.Empty;

            if (openFileDialogFactures.ShowDialog() == DialogResult.OK)
            {
                Facture nouvelleFacture = Facture.ChargerFichier(openFileDialogFactures.FileName);

                if (nouvelleFacture != null)
                {
                    DictionnaireDonnees.ListeFacture.Add(nouvelleFacture);

                    if (ControleFacture.Visible)
                    {
                        ControleFacture.UpdateListeFactures();
                    }
                    else
                    {
                        ControleArticle.ChargerInterface(nouvelleFacture);
                    }
                }
            }
        }

        public void AfficherArticle(bool InterfaceArticle, Facture facture = null)
        {
            if (InterfaceArticle)
            {
                ControleFacture.Visible = false;
                ControleArticle.Visible = true;

                ControleArticle.ChargerInterface(facture);

                ControleArticle.BringToFront();
            }
            else
            {   
                ControleArticle.Visible = false;
                ControleFacture.Visible = true;

                ControleFacture.BringToFront();
            }
        }

        private void sauvegarderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Facture factureASauvegarder = null;

            if (ControleFacture.Visible)
            {
                factureASauvegarder = ControleFacture.GetFactureSelected();
            }
            else
            {
                factureASauvegarder = ControleArticle.GetFacture();
            }

            if (factureASauvegarder != null)
            {
                saveFileDialogFactures.FileName = "Facture_" + factureASauvegarder.NoFacture;

                if (saveFileDialogFactures.ShowDialog() == DialogResult.OK)
                {
                    factureASauvegarder.Enregistrer(saveFileDialogFactures.FileName);
                }
            }
        }
    }
}
